package com.xuecheng.content.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.CategoryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.TeachplanVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.CoursePub;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.mappers.CoursePubMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.PublishPageService;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.media.client.CategoryClient;
import com.xuecheng.web.exceptions.BusinessException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 课程发布 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-16
 */
@Service
public class CoursePubServiceImpl extends ServiceImpl<CoursePubMapper, CoursePub> implements CoursePubService {

    @Autowired
    private CourseBaseService courseBaseService;
    @Autowired
    private TeachplanService teachplanService;
    @Autowired
    private CategoryClient categoryClient;

    public Map preview(Long courseBaseId) {
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        CoursePub coursePub = BeanHelper.copyProperties(courseBase, CoursePub.class);
        ResponseResult<TeachplanVo> result = teachplanService.treeNodes(courseBaseId);
        TeachplanVo teachplanVo = result.getData();

        coursePub.setCourseId(courseBaseId);
        coursePub.setTeachplan(JSON.toJSONString(teachplanVo));

        String mtName = getCategoryName(courseBase.getMt());
        String stName = getCategoryName(courseBase.getSt());
        coursePub.setMtName(mtName);
        coursePub.setStName(stName);

        Map<String,Object> map = new HashMap();
        map.put("coursePub",coursePub);
        map.put("teachplanNode",teachplanVo);
        return map;
    }



    private String getCategoryName(String id) {
        ResponseResult<CategoryVo> result = categoryClient.findById(id);
        if(result.getCode() != ErrorCode.SUCCESS.getCode()) {
            throw new BusinessException(ErrorCode.ERROR);
        }
        return result.getData().getName();
    }
    @Autowired
    private PublishPageService publishPageService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Transactional(rollbackFor = Exception.class)
    public ResponseResult publish(Long courseBaseId) throws Exception {
        Map map = this.preview(courseBaseId);
        CoursePub coursePub = (CoursePub)map.get("coursePub");
        coursePub.setId(null);

        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        courseBase.setAuditStatus(CourseConstants.AUDIT_PUBLISHED_STATUS);
        boolean update = courseBaseService.updateById(courseBase);
        if (!update) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }

        LambdaQueryWrapper<CoursePub> qw = new LambdaQueryWrapper<>();
        qw.eq(CoursePub ::getCourseId,courseBaseId);
        CoursePub one = this.getOne(qw);
        if (one == null) {
            this.save(coursePub);
        }else {
            coursePub.setId(one.getId());
            this.updateById(coursePub);
        }
        publishPageService.publishPage(map,courseBaseId);
        rabbitTemplate.convertAndSend("content-exchange","course.pub",coursePub.getId());
        return ResponseResult.okResult();
    }
}
